﻿using System;
using System.Net;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Infrastructure.Communication;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.CMS.Models;
using MessageModel = Sharp.WebBooter.Areas.Platform.Models.MessageModel;

namespace Sharp.WebBooter.Areas.CMS.Controllers
{
    public class MessageController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter parameters)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessagePaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        // GET: Manage/Employe/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MessageDTO dto = null;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                dto = proxy.Channel.GetMessageInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return View("Edit", dto.To<MessageModel>());
        }

        // GET: Manage/Employe/Create
        public ActionResult Create()
        {
            return View("Edit", new MessageModel());
        }


        // GET: Manage/User/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MessageDTO dto = null;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                dto = proxy.Channel.GetMessageInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return View("Edit", dto.To<MessageModel>());
        }


        [HttpPost]
        public ActionResult Save(MessageModel user)
        {
            MessageDTO dto = user.To<MessageDTO>();

            using (var proxy = new ServiceProxy<IUserService>())
            {
                var res = proxy.Channel.SendMessage(dto);

                return this.JsonNet(res);
            }
        }

        [HttpPost]
        public ActionResult Delete(Guid[] ids)
        {
            var res = new JsonResult();
            using (var proxy = new ServiceProxy<IUserService>())
            {
                res.Data = proxy.Channel.DeleteMessages(ids);
            }

            return res;
        }
    }
}